package cn.zqh.router.config;

import cn.hutool.json.JSONObject;
import jdk.nashorn.internal.parser.JSONParser;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;

/**
 * @Author: zqh
 * @Date: 2023-03-11-11:42
 * @Description: 监听队列$
 */
@Component
public class MyConsumer {

    @Value("${spring.mail.username}")
    private String username;   // 邮件发送人

    @Resource
    private JavaMailSenderImpl mailSender;
    /**
      * @Author: zqh
      * @Date:
      * @Description: 监听队列
    */
    @RabbitListener(queues = "ms_queue2")
    public void process(Message message){
        byte[] body = message.getBody();
        JSONObject entries = new JSONObject(body);
        String email = entries.getStr("email");
        String code = entries.getStr("code");
        //发送邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("尊敬的用户您好：");  //头文件
        mailMessage.setText("您的验证码为：" + code + "；请于4分钟内完成验证码的校验。");  //内容
        mailMessage.setTo(email);   //接收者
        mailMessage.setFrom(username);  //发送者
        mailSender.send(mailMessage);
    }
}
